/*
* @Author: dengjiayao
* @Date:   2017-04-10 14:41:56
* @Last Modified by:   dengjiayao
* @Last Modified time: 2017-04-24 15:34:32
*/
import Pages from '../../../pages/pages.vue'
import { showMsg, getBetaAdList, deleteBetaAd } from '../../../../vuex/action'

export default {
  name: 'article-gao-list',
  components: {
    'vue-pages': Pages
  },
  data() {
    return {
      list: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      }
    }
  },
  ready() {
    this.getList({})
  },
  vuex: {
    getters: {
      fresh: state => state.gaoListNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xGetList: getBetaAdList,
      xDelete: deleteBetaAd
    }
  },
  methods: {
    getList({start = this.pages.start, limit = this.pages.limit}) {
      this.xGetList({
        start: start,
        limit: limit
      })
        .then((data) => {
          this.list = data.list
          this.pages.totalPages = Math.ceil(data.totalCount / limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    updateAd(ad) {
      var configModal = this.$root.$refs.articleGaoConfig
      configModal.actionType = 'UPDATE'
      configModal.ad = ad
      configModal.showModal = true
    },
    deleteAd(betaId) {
      this.xDelete({
        betaId: betaId
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.getList({})
      }
    }
  }
}
